'use strict';

const Service = require('egg').Service;

class HomeService extends Service {
    /**
     * 根据手机号查询用户
     * @param {*} mobile
     */
    async findByMobile(mobile) {
        const sql = 'select * from user where mobile = ? limit 0, 1';
        const params = [mobile];
        const res = await this.app.mysql.query(sql, params);
        return res[0];
    }

    /**
     * 查询某个用户<user>的前端权限<acl>
     * @param {*} id
     */
    async getMenusById(id) {
        const sql = `
            select acl.id, acl.name, acl.path
            from acl
                join r_role_acl on acl.id = r_role_acl.acl_id
                join r_user_role on r_role_acl.role_id = r_user_role.role_id
            where acl.type = 1 and r_user_role.user_id = ?
        `;
        const params = [id];
        const res = await this.app.mysql.query(sql, params);
        return res;
    }

    /**
     * 用户详情
     * @param {*} id
     * @returns
     */
    async getUserDetail(id) {
        const sql = `
            select u.id, u.mobile, u.nickname, u.email, u.create_time, r.id as role_id, r.name as role_name
            from user as u
                join r_user_role as rur on u.id = rur.user_id
                join role as r on rur.role_id = r.id
            where u.id = ?
        `;
        const params = [id];
        const res = await this.app.mysql.query(sql, params);
        return res[0];
    }
}

module.exports = HomeService;
